<template>
    <div class="addContaions" >
        <div class="ssheader">
            <div class="sys">
                <input type="text" placeholder="搜一搜" v-model="sysInp" @keyup.enter="sys()">
                <span class="iconfont icon-search mouse" @click="sys()" ></span>

            </div>
            <div class="body" v-load="isloading">
                <div class="sysList" v-if="returnList">
                    <img :src="sysFriend.img" alt="">
                    <div class="sysdetail">
                        <p>{{ sysFriend.name }}</p>
                        <p class="qm">{{ sysFriend.signature }}</p>
                    </div>
                    <el-button class="b addBtn" @click="add(sysFriend.id)">添加</el-button>
                </div>
                <el-empty v-if="!returnList" :description="description" class="empty"></el-empty>
            </div>

        </div>
        <p class="hyqq">好友请求</p>
        <div class="ssfooter" v-load="qqLoad">

            <ul v-if="chatStore.verifyMsg.length !== 0">
                <li v-for="item in chatStore.verifyMsg" :key="item.id">
                    <img :src="item.img" alt="">
                    <div>
                        <p>{{ item.name }}</p>
                        <p class="qm">{{ item.text }}</p>
                    </div>
                     <el-button class="b okBtn" @click="agreeFriend(item.id,item.veryMsgId)" v-if="item.type === '104'">接收</el-button>
                    <el-button class="b noBtn" @click="noAgreeFriend(item.id,item.veryMsgId)" v-if="item.type === '104'">拒绝</el-button>
                    <el-text class="status mx-1" v-if="item.type !== '104'">{{ item.type === '101'?'已添加': (item.type === '102'?'已拒绝':(item.type === '105'?'对方已拒绝':'验证中')) }}</el-text> 
                    <!-- <el-text class="status mx-1" v-if="">{{ 123 }}</el-text>  -->

                     

                </li>
             
                <!-- <li>
                    <img src="@/assets/bg-img/mr.jpg" alt="">
                    <div>
                        <p>lfy</p>
                        <p class="qm">几多轮回少一人</p>
                    </div>
                    <el-button class="b okBtn">接收</el-button>
                    <el-button class="noBtn">拒绝</el-button>
                </li> -->
             
            </ul>
            <el-empty v-if="chatStore.verifyMsg.length === 0" description="暂时没有好友请求"></el-empty>

        </div>
    </div>
    <el-dialog   v-model="dialogTableVisible" title="验证消息" style="background-color: rgb(87, 93, 95);width: 550px;height: 350px;" >
        <!-- <el-text class="mx-1" type="info">验证消息</el-text> -->
        <textarea  cols="30" rows="8" v-model="textMsg" > 

        </textarea>
        <template #footer>
            <span class="dialog-footer">
                <el-button class="b Btn" @click="dialogTableVisible = false" >取消</el-button>
                <el-button class="b Btn" type="primary" @click="sendAddAxios">
                    发送
                </el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script setup>
import { defineOptions, reactive, ref,onMounted} from 'vue';
import { findFriednService,addFriendSerivce,agreeFriendService,noAgreeFriendService } from '@/api/friend.js'
import { ElMessage,ElLoading  } from 'element-plus';
import {useChatStore,useFriendStore} from '@/stores'
import {getFriendsService,getVerifyMsgService,getNowChatListService} from '@/api/chat.js'

const chatStore = useChatStore();
const friendStore = useFriendStore();
const returnList = ref(false);
const isloading = ref(false);
// const addmsg = ref(false);
const description = ref('试试搜索吧');
const sysInp = ref('');
const sysFriend = ref('')
const dialogTableVisible = ref(false)
const textMsg = ref('')
const addId = ref('')
const qqLoad = ref(false)
// const test = ()=>{
//     isloading.value = true;
//     setTimeout(()=>{
//         isloading.value = false;
//     },1000)
// }
onMounted(()=>{

})
// 添加好友
const addFriendEntity = reactive({
    to:'',
    msg:''
})
const add = (id) => {
    addId.value = id;
    dialogTableVisible.value = true
    console.log(id)
}
const sendAddAxios =async ()=>{
    addFriendEntity.to = addId;
    addFriendEntity.msg = textMsg;
    console.log(addFriendEntity)
    const {data:res} = await addFriendSerivce(addFriendEntity)

    if(res.code !== 200){
        dialogTableVisible.value = false;
        textMsg.value = '';
        addId.value = '';
        return ElMessage.error(res.msg)
    }
    textMsg.value = '';
    addId.value = ''
    dialogTableVisible.value = false;
    ElMessage.success(res.msg);
    const {data:verifyRes} = await getVerifyMsgService(); 
   chatStore.setVerifyMsg(verifyRes.data);
}
//同意好友申请
const agreeFriend =async (id,veryMsgId)=>{
    let agreeFriendReq = {
        "id":id,
        "veryMsgId":veryMsgId
    }
    qqLoad.value = true;
    const {data:res} = await agreeFriendService(agreeFriendReq)
    if(res.code !== 200){
      return  ElMessage.error(res.msg);
    }
    const {data:fres} = await getFriendsService();
   friendStore.setFriend(fres.data);

   const {data:verifyRes} = await getVerifyMsgService(); 
   chatStore.setVerifyMsg(verifyRes.data);
   const {data:nowChatResp} = await getNowChatListService();
   chatStore.setNowChatFriends(nowChatResp.data);
   chatStore.setNewFriend(id);

   qqLoad.value = false;

   return ElMessage.success('添加成功');

}

//拒绝好友申请
const noAgreeFriend =async (id,veryMsgId)=>{
    let noAgreeFriendReq = {
        "id":id,
        "veryMsgId":veryMsgId
    }
    qqLoad.value = true;

    const {data:res} = await noAgreeFriendService(noAgreeFriendReq);
    if(res.code !== 200){
      return  ElMessage.success(res.msg);
    }
    const {data:verifyRes} = await getVerifyMsgService(); 
   chatStore.setVerifyMsg(verifyRes.data);
   qqLoad.value = false;
  return ElMessage.success(res.msg);
}
const sys = async () => {
    isloading.value = true;
    const { data: res } = await findFriednService(sysInp.value);
    isloading.value = false;

    if (res.code !== 200) {
        return ElMessage.error(res.msg);
    }
    sysFriend.value = res.data;
    if (!res.data) {
        returnList.value = false;
        description.value = "查无此人哦~";
        sysInp.value = '';
        return ElMessage.success("没有这个用户")
    }
    sysInp.value = '';
    returnList.value = true;
    ElMessage.success(res.msg);
}
defineOptions({
    name: 'addfriend'
})
</script >

<style lang="less" scoped>
.dialog{
    position: relative;
}
.Btn{
        right: 0;
        width: 60px;
        height: 28px;
        background-color: #443f3f;
        color: #ada7a7;
        border: none;
}
textarea{
    margin: 0.5% 1%;
          border: 0; //去除边框
          background-color: #9e9d9d; //透明
          outline: none; //去除鼠标获取焦点时的边框
          width: 80%;
          height: 20%;
          font-size: 18px;
          font-family: "宋体";
          color: #070707;

}
.addContaions {
    background-color: rgb(54, 57, 58);
    // background-color: rgb(87, 93, 95);
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    padding: 2%;
    min-width: 600px;

    .body {
        height: 295px;
        // background-color: pink;
        position: relative;
    }

    .empty {
        // width: 520px;
        // height: 34px;
        // min-height: 300px;

    }

    .sys {
        width: 520px;
        box-shadow: 0px, 1px, 2px, 0px, rgb(211, 211, 211, 0.5);
        border-bottom: 2px solid #f4f4f4;
        display: flex;

        height: 34px;

        input {
            // width: 100%;
            // height: 100%;
            border: 0;
            background-color: transparent;
            outline: none;
            flex: 1;
            color: #CCCCCC;
        }

        input::placeholder {
            font-size: 14px;
            color: #CCCCCC
        }

        span {
            margin-top: 5px;
            vertical-align: middle;
            font-size: 20px;
        }
    }

    .sysList {
        display: flex;
        margin-top: 30px;
        width: 500px;
        position: relative;
        // background-color: pink;
        border-bottom: #CCCCCC;

        img {
            width: 50px;
            height: 50px;
            margin-right: 10px;
        }

        .sysdetail {
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            text-align: left;
        }

        .addBtn {
            position: absolute;
            right: 0;
            margin-top: 4px;
            background-color: #443f3f;
            color: #ada7a7;
            border: none;
        }
    }

    .hyqq {
        text-align: left;
        margin-right: 450px;
        margin-top: 10px;
    }

    .ssfooter {
        width: 550px;
        // background-color: pink;
        text-align: left;
        font-size: 16px;
        color: #CCCCCC;
        // margin-top:30px;  
        margin-left: 30px;
        flex: 1;
        position: relative;
    }

    ul {
        margin-top: 15px;

        li {
            display: flex;
            position: relative;
            // background-color: pink;
            align-items: center;
            margin-bottom: 10px;
            border-bottom: 1px solid #534e4e;
            padding: 1%;

            div {
                height: 50px;

                display: flex;
                flex-direction: column;
                justify-content: space-between;
                // background-color: pink;
            }

        }
    }

    img {

        width: 50px;
        height: 50px;
        margin-right: 10px;
    }

    .okBtn,
    .noBtn {
        position: absolute;
        right: 0;
        width: 60px;
        height: 28px;
        background-color: #443f3f;
        color: #ada7a7;
        border: none;

    }
    .status{
         position: absolute;
        right: 0; 
    }
    .okBtn:hover,
    .noBtn:hover {
        border-color: #252424;
    }

    .okBtn {
        transform: translateX(-110%);
    }

}

.qm {
    color: #9e9d9d;
    font-size: 13px;
}</style>